import { ref } from "vue";

export const useRequest = <T = any>(service: () => T) => {
  const data = ref<T>();
  const loading = ref<boolean>(true);
  const fetch = async () => {
    try {
      loading.value = true;
      const res = await service();
      loading.value = false;
      data.value = res;
    } catch (error) {
      loading.value = false;
    }
  };
  fetch();
  return {
    data,
    loading,
  };
};
